/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2jserver.gameserver.custom;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

/**
 * @author Home
 *
 */
public class CustomConfig
{
	private static final Logger _log = Logger.getLogger(CustomConfig.class.getName());
	
	public int CRYSTAL_REWARD_ID = 57;
	public int CRYSTAL_REWARD_COUNT = (int) 1E6;
	public boolean CRYSTAL_REWARD_ENABLED = false;
	public int SPREE_REWARD_ID = 57;
	public int SPREE_REWARD_COUNT = (int) 1E6;
	public boolean SPREE_REWARD_ENABLED = false;
	
	private CustomConfig()
	{
		load();
	}
	
	/**
	 * 
	 */
	private void load()
	{
		try
		{
			File file = new File("./config/custom.xml");
			if (!file.exists())
			{
				_log.severe("config" + File.pathSeparator + "custom.xml is missing. Loading default values!");
				return;
			}
			Document doc = null;
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			factory.setValidating(false);
			factory.setIgnoringComments(true);
			doc = factory.newDocumentBuilder().parse(file);
			for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
			{
				if ("list".equalsIgnoreCase(n.getNodeName()))
				{
					for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
					{
						if ("SpreeReward".equalsIgnoreCase(d.getNodeName()))
						{
							String itemId = d.getAttributes().getNamedItem("itemId").getNodeValue();
							String count = d.getAttributes().getNamedItem("count").getNodeValue();
							String enabled = d.getAttributes().getNamedItem("enabled").getNodeValue();
							SPREE_REWARD_ID = Integer.parseInt(itemId);
							SPREE_REWARD_COUNT = Integer.parseInt(count);
							SPREE_REWARD_ENABLED = Boolean.parseBoolean(enabled);
						}
						else if ("CrystalReward".equalsIgnoreCase(d.getNodeName()))
						{
							String itemId = d.getAttributes().getNamedItem("itemId").getNodeValue();
							String count = d.getAttributes().getNamedItem("count").getNodeValue();
							String enabled = d.getAttributes().getNamedItem("enabled").getNodeValue();
							CRYSTAL_REWARD_ID = Integer.parseInt(itemId);
							CRYSTAL_REWARD_COUNT = Integer.parseInt(count);
							CRYSTAL_REWARD_ENABLED = Boolean.parseBoolean(enabled);
						}
					}
				}
			}
		}
		catch (Exception e)
		{
			_log.log(Level.SEVERE, null, e);
		}
	}
	
	public static CustomConfig getInstance()
	{
		return SingletonHolder._instance;
	}
	
	private static class SingletonHolder
	{
		protected static final CustomConfig _instance = new CustomConfig();
	}
}
